package net.vinote.sosa.rmi;

import java.io.IOException;
import java.util.Properties;

import net.vinote.smart.socket.lang.QuicklyConfig;
import net.vinote.smart.socket.logger.RunLogger;
import net.vinote.smart.socket.protocol.P2PProtocolFactory;
import net.vinote.smart.socket.protocol.p2p.filter.SecureFilter;
import net.vinote.smart.socket.protocol.p2p.message.BaseMessageFactory;
import net.vinote.smart.socket.protocol.p2p.message.HeartMessageReq;
import net.vinote.smart.socket.protocol.p2p.message.LoginAuthReq;
import net.vinote.smart.socket.protocol.p2p.message.RemoteInterfaceMessageReq;
import net.vinote.smart.socket.protocol.p2p.message.SecureSocketMessageReq;
import net.vinote.smart.socket.protocol.p2p.processor.HeartMessageProcessor;
import net.vinote.smart.socket.protocol.p2p.processor.LoginAuthProcessor;
import net.vinote.smart.socket.protocol.p2p.processor.RemoteServiceMessageProcessor;
import net.vinote.smart.socket.protocol.p2p.processor.SecureSocketProcessor;
import net.vinote.smart.socket.protocol.p2p.server.P2PServerMessageProcessor;
import net.vinote.smart.socket.service.filter.SmartFilter;
import net.vinote.smart.socket.service.filter.impl.FlowControlFilter;
import net.vinote.smart.socket.service.manager.ServiceProcessorManager;
import net.vinote.smart.socket.service.process.ProtocolDataProcessor;
import net.vinote.smart.socket.transport.nio.NioQuickServer;

public class RmiServer {
	private NioQuickServer server;
	private int port;

	public void init() throws ClassNotFoundException {
		// 定义服务器接受的消息类型以及各类消息对应的处理器
		Properties properties = new Properties();
		properties.put(HeartMessageReq.class.getName(), HeartMessageProcessor.class.getName());
		properties.put(RemoteInterfaceMessageReq.class.getName(), RemoteServiceMessageProcessor.class.getName());
		properties.put(LoginAuthReq.class.getName(), LoginAuthProcessor.class.getName());
		properties.put(SecureSocketMessageReq.class.getName(), SecureSocketProcessor.class.getName());
		BaseMessageFactory.getInstance().loadFromProperties(properties);

		QuicklyConfig config = new QuicklyConfig(true);
		P2PProtocolFactory factory = new P2PProtocolFactory();
		config.setProtocolFactory(factory);
		config.setFilters(new SmartFilter[] { new FlowControlFilter(), new SecureFilter() });
		config.setPort(port);
		ProtocolDataProcessor processor = new P2PServerMessageProcessor();
		config.setProcessor(processor);// 定义P2P协议的处理器,可以自定义
		server = new NioQuickServer(config);
		try {
			server.start();
		} catch (IOException e) {
			RunLogger.getLogger().log(e);
		}
	}

	public void publishService(String key, Object service) {
		RemoteServiceMessageProcessor processor = (RemoteServiceMessageProcessor) ServiceProcessorManager.getInstance()
			.getProcessor(RemoteInterfaceMessageReq.class);
		processor.registService(key, service);
	}

	public void destory() {
		server.shutdown();
	}

	/**
	 * Setter method for property <tt>port</tt>.
	 *
	 * @param port
	 *            value to be assigned to property port
	 */
	public final void setPort(int port) {
		this.port = port;
	}

}
